/**
 * https://leetcode.cn/submissions/detail/558818518/
 * 2958. 最多 K 个重复元素的最长子数组
 * medium 黄伟杰 2024.8.26
 * 滑动窗口
 */

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int maxSubarrayLength(vector<int> &nums, int k)
    {
        unordered_map<int, int> hash;
        int i = 0, j = 0, ans = 0;
        for (; j < nums.size(); j++)
        {
            hash[nums[j]]++;
            while (hash[nums[j]] > k)
            {
                hash[nums[i]]--;
                i++;
            }
            ans = max(ans, j - i + 1);
        }
        return ans;
    }
};